package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author liuyaning
 * @version 1.0
 * @date 2021/7/30 17:36
 */
@Service
@Transactional
@Slf4j
public class AdSenSitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto sensitiveDto) {
        //判断参数
        if(sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //初始化分页参数
        sensitiveDto.checkParam();
        //判断name是否为空，选择是否拼接参数
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            wrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        //设置分页
        IPage page = new Page(sensitiveDto.getPage(),sensitiveDto.getSize());
        page(page,wrapper);
        //响应数据
        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(), (int) page.getTotal());
        //获取data
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //判断参数
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断该敏感词是否已存在
        AdSensitive sensitive = getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //设置其他字段
        adSensitive.setCreatedTime(new Date());
        //保存敏感词
        save(adSensitive);
        //响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数判断
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据id判断是否存在
        AdSensitive sensitive = getById(adSensitive.getId());
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行更新
        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断该信息是否存在
        AdSensitive sensitive = getById(id);
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行删除
        removeById(id);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
